package com.wangyang.service.Impl;

import com.wangyang.mapper.UserMapper;
import com.wangyang.pojo.Result;
import com.wangyang.pojo.User;
import com.wangyang.service.UserService;
import com.wangyang.utils.JwtUtil;
import com.wangyang.utils.Md5Util;
import com.wangyang.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtUtil jwtUtil;
    @Override
    public Result register(String username, String password) {
        //检查用户名是否占用
        User u = userMapper.getUserByUsername(username);
        if(u==null){
            //用户名可用
            //密码进行MD5加密
            String md5password = Md5Util.getMD5String(password);
            //注册用户
            userMapper.addUser(username, md5password);
            return Result.success();
        }
        else {
            //用户名已被占用
            return Result.error("用户名已被占用");
        }

    }

    @Override
    public Result login(String username, String password) {
        //根据username查询用户
        User user = userMapper.getUserByUsername(username);
        if(user==null){
            //用户不存在
            return Result.error("用户不存在");
        }
        //检查密码是否正确
        if(!Md5Util.checkPassword(password, user.getPassword())){
            //密码不正确
            return Result.error("密码错误");
        }
        //生成token并返回
        String token = jwtUtil.createToken(user.getId().longValue());
        return Result.success(token);
    }

    @Override
    public Result getUserInfo() {
        //从ThreadLocal中找到用户id
        Integer id = ThreadLocalUtil.get();

        //根据用户id查找用户并返回
        User user = userMapper.getUserById(id);
        return Result.success(user);
    }

    @Override
    public Result updateUserInfo(User user) {
        //从ThreadLocal中找到用户id
        Integer id = ThreadLocalUtil.get();
        //判断修改的用户id时候与登陆用户相同
        if(!id.equals(user.getId())){
            //不同
            return Result.error("用户id错误，无权限修改其他用户信息");
        }
        userMapper.updateUser(user);
        return Result.success();
    }

    @Override
    public Result updateUserPic(String userPic) {
        //从ThreadLocal中找到用户id
        Integer id = ThreadLocalUtil.get();
        userMapper.updateUserPic(id, userPic);
        return Result.success();
    }

    @Override
    public Result updateUserPassword(String oldpwd, String newpwd, String repwd) {
        //从ThreadLocal中找到用户id
        Integer id = ThreadLocalUtil.get();
        //id查询用户数据
        User user = userMapper.getUserById(id);
        //比较旧密码是否正确
        if(!Md5Util.checkPassword(oldpwd, user.getPassword())){
            return Result.error("原密码错误");
        }
        //比较新密码两次填写是否正确
        if(!newpwd.equals(repwd)){
            return Result.error("两次填写的新密码不一致");
        }
        //更新新密码
        String md5newpwd = Md5Util.getMD5String(newpwd);
        userMapper.updateUserPassword(id, md5newpwd);
        return Result.success();
    }

}
